package cn.sczhyt.modules.app.controller;

import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.app.annotation.Login;
import cn.sczhyt.modules.app.annotation.LoginUser;
import cn.sczhyt.modules.app.entity.UserEntity;
import cn.sczhyt.modules.app.utils.FileReadOnlineUtils;
import cn.sczhyt.modules.szhy.entity.MeetingTopicsFileEntity;
import cn.sczhyt.modules.szhy.entity.MeetingTopicsUserEntity;
import cn.sczhyt.modules.szhy.service.MeetingTopicsFileService;
import cn.sczhyt.modules.szhy.service.MeetingTopicsUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * 议题文件
 *
 * @author luojialin
 * @email 78357966@qq.com
 * @date 2021-05-18 10:39:50
 */
@Api(tags = "APP端议题文件")
@RestController
@RequestMapping("app/meetingtopicsfile")
public class AppMeetingTopicsFileController {
    @Autowired
    private MeetingTopicsFileService meetingTopicsFileService;
    @Autowired
    private MeetingTopicsUserService meetingTopicsUserService;
    /**
     * 列表
     */
    @ApiOperation("获取议题文件列表")
    @RequestMapping("/list/{topicsId}")
    @Login
    public R list(@PathVariable("topicsId") String topicsId,@LoginUser UserEntity user){

        QueryWrapper<MeetingTopicsUserEntity> wrapper = new QueryWrapper<>();

        List<MeetingTopicsUserEntity> userEntities = meetingTopicsUserService.list(wrapper.eq("topics_id", topicsId).like("user_id", '%' + user.getUserId() + '%'));

        if (userEntities.isEmpty()){
            return null;
        }
        QueryWrapper<MeetingTopicsFileEntity> queryWrapper = new QueryWrapper<>();
        List<MeetingTopicsFileEntity> list = meetingTopicsFileService.list(queryWrapper.eq("topics_id", topicsId));

        return R.ok().put("data", list);
    }


    /**
     * 议题文件在线阅读
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("会议文件在线阅读")
    @RequestMapping("/topicsFileReadOnline/{id}")
    public R topicsFileReadOnline(HttpServletResponse response,
                                  @PathVariable("id") String id) throws IOException {

        MeetingTopicsFileEntity topicsFileEntity = meetingTopicsFileService.getById(id);
        String filePath = topicsFileEntity.getFilePath();
        String fileName = topicsFileEntity.getFileName();
        String fileType = "pdf";
        FileReadOnlineUtils.fileReadOnline(response,filePath,fileName,fileType);
        return R.ok();
    }

}
